<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 输入框 -->
      <div>
        <el-row :gutter="20">
          <el-col :span="7">
            <el-input placeholder="请输入内容" clearable @clear="getGoodList" v-model="queryInfo.query">
              <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="6"> <el-button type="primary" @click="goAddGood">添加商品</el-button></el-col>
        </el-row>
      </div>
      <!-- table -->
      <el-table :data="goodList" stripe style="width: 100%" border>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="530px"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="108px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="156px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="openEditDialog(scope.row.goods_id)"></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="openDel(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- page -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodList: []
    }
  },
  methods: {
    async getGoodList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res.data)
      if (res.meta.status !== 200) {
        return this.$message.error('获取列表数据失败!')
      }
      this.$message.success('获取列表数据成功!')
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodList()
    },
    // 点击确定按钮删除用户
    async openDel(id) {
      // console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => {
        return err
      })
      console.log(confirmResult)
      // 如果用户确认删除返回结果为字符串confirm
      // 如果用户取消删除返回结果为字符串cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // console.log('queren')
      const { data: res } = await this.$http.delete(`goods/${id}`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败!')
      }
      this.$message.success('删除商品成功!')
      this.getGoodList()
    },
    // 点击添加商品跳转goAddGood
    goAddGood() {
      this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGoodList()
  }
}
</script>

<style lang="scss" scoped></style>
